//
//  DownloadManager.m
//  Leisure
//
//  Created by lanou3g on 16/4/12.
//  Copyright © 2016年 wenze. All rights reserved.
//

#import "DownloadManager.h"

@interface DownloadManager ()<DownLoadDelegate>
// 用于管理存储下载任务
@property (nonatomic,strong)NSMutableDictionary *dic;

@end

@implementation DownloadManager

- (NSMutableDictionary *)dic{
    if (!_dic) {
        _dic = [[NSMutableDictionary alloc]init];
    }
    return _dic;
}

+ (instancetype)defaultManager{
    static DownloadManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[DownloadManager alloc]init];
    });
    return manager;
}

- (DownLoad *)addDownloadWithUrl:(NSString *)url{
    DownLoad *down = [[DownLoad alloc]initWithUrl:url];
    down.delegate = self;
    [down didFinishWithFinishLoad:^(NSString *filePath, NSString *url) {
        
    } andDownloading:^(float progress, long speed) {
        
    }];
    [self.dic setValue:down forKey:url];
    return down;
}

- (DownLoad *)getDownloadWithUrl:(NSString *)url{
    return [self.dic valueForKey:url];
}

#pragma mark -  DownloadDelegate
- (void)didFinishLoadWithUrl:(NSString *)url{
    // 移除当前任务 放弃持有 释放内存
    if ([self.dic valueForKey:url]) {
        [self.dic removeObjectForKey:url];
    }
}






@end
